package com.hxz.servlet;

import com.alibaba.fastjson.JSON;
import com.hxz.entity.User;
import com.hxz.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 接口层
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        //1.设置字符集
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //2.根据请求的参数method，跳到指定方法
        String method = req.getParameter("method");
        if ("getUsers".equals(method)) {
            getUsers(req, resp);
        } else if ("addUser".equals(method)) {
            addUser(req, resp);
        } else if ("editUser".equals(method)) {
            editUser(req, resp);
        } else if ("deleteUser".equals(method)) {
            deleteUser(req, resp);
        } else if ("getOne".equals(method)) {
            getOne(req, resp);
        }
    }
    //根据用户id 获取用户记录
    private void getOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        User user = userService.getOne(id);
        resp.getWriter().write(JSON.toJSON(user).toString());
    }
    //根据id删除用户记录
    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        userService.deleteUser(id);
    }
    //修改用户
    private void editUser(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");

        User user = new User(id, username, password, phone);
        userService.editUser(user);
    }

    //添加用户
    private void addUser(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        //有参构造
        User user = new User(0, username, password, phone);
        userService.addUser(user);
    }

    // 获取用户信息
    private void getUsers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //参数赋值
        String username = req.getParameter("username");
        int pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        List<User> userList = userService.getUsers(username, pageIndex, pageSize);
        resp.setHeader("Content-Type", "text/html;charset=UTF-8");
        resp.getWriter().write(JSON.toJSON(userList).toString());
    }
}
